const { 
  getInfo,
  getInfoCount,
  createInfo,
  destroyInfo,
  appLogin,
  getInfoAll
 } = require('../service/info'),
  { returnInfo } = require('../lib/utils'),
  { nanoid } = require('nanoid'),
  { API, LOGIN } = require('../config/err_config');


class Info {
   async Login(ctx,next){
    try {
      const {code} = ctx.request.body
      let returnMess = await appLogin(code);
      ctx.body = returnInfo(API.APPLOGIN_SUCCESS, returnMess);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.APPLOGIN_ERROR)
    }
  }
  async getInfoAll(ctx) {
    try {
      let returnMess = await getInfoAll();
      ctx.body = returnInfo(API.GET_INFO_SUCCESS, returnMess);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_INFO_ERROR)
    }
  }
  async getInfo(ctx, next) {
    try {
      const { tid, offset, limit = 10 } = ctx.request.body
      let returnMess = await getInfo(tid, Number(offset), Number(limit));
      ctx.body = returnInfo(API.GET_INFO_SUCCESS, returnMess);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_INFO_ERROR)
    }

  }
  async getInfoCount(ctx) {
    try {
      let count = await getInfoCount();
      ctx.body = returnInfo(API.GET_INFO_COUNT_SUCCESS, count[0].dataValues.count);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_INFO_COUNT_ERROR)
    }
  }
  async createInfo(ctx, next) {
    try {
      let infoData = ctx.request.body;
      let returnMess = await createInfo(infoData);
      ctx.body = returnMess ? returnInfo(API.CREATE_INFO_SUCCESS,returnMess)
        : returnInfo(API.CREATE_INFO_ERROR)
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.CREATE_INFO_ERROR)
    }


  }
  async destroyInfo(ctx) {
    let { id } = ctx.request.body;
    try {
      let returnMess = await destroyInfo(id);
      ctx.body = returnMess ? returnInfo(API.DESTORY_INFO_SUCCESS)
        : returnInfo(API.DESTORY_INFO_ERROR)
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.DESTORY_INFO_ERROR)
    }


  }
  

  
}
module.exports = new Info();